package cn.leedsoft.luma.controller;

import cn.leedsoft.luma.common.Reply;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableMap;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

/**
 * Created by leeds on 2016/7/5 15:46.
 */
@Aspect
@Component
public class ControllerAop {

    @Around("execution(* cn.leedsoft.luma.controller..*Controller.*Valid(..))")
    public Object handle(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        if (joinPoint.getSignature().getName().endsWith("Valid")) {//validparams
            for (Object arg : args) {
                if (arg != null && arg instanceof BindingResult) {
                    BindingResult bindingResult = (BindingResult) arg;
                    Reply.valid(bindingResult);
//                    if (bindingResult.hasErrors()) {
//                        throw new RuntimeException(JSON.toJSONString(bindingResult.getFieldErrors()));
//                    }
                }
            }
        }
        Object retVal = joinPoint.proceed(args);
        return retVal;
    }
}
